#include "BasePlayer.h"
#include "BtOgrePG.h"
#include "BtOgreGP.h"
#include "BtOgreExtras.h"
#include "OgreConsole.h"
#include "GameState.hpp"
using namespace Ogre;


// declaration of callback
/*
struct btDrawingResult : public btCollisionWorld::ContactResultCallback
{
	BasePlayer* origin;
 
	btDrawingResult(BasePlayer* player){
		origin = player;
	}

   virtual   btScalar   addSingleResult(btManifoldPoint& cp,   const btCollisionObject* colObj0,int partId0,int index0,const btCollisionObject* colObj1,int partId1,int index1)
   {
      OgreConsole::getSingleton().print("contact!");
      btVector3 ptA = cp.getPositionWorldOnA();
      btVector3 ptB = cp.getPositionWorldOnB();
      btVector3 norm = cp.m_normalWorldOnB;
	  origin->doJump();
      return 0;
   }
};
*/

BasePlayer::BasePlayer(Ogre::String name, PlayerNumber pNumber, Ogre::ColourValue pColour, Ogre::Vector3 pPos, Ogre::SceneManager* sceneMgr)
{
	int size = 2.6;

	m_PlayerNumber = pNumber;
	OgreConsole::getSingleton().print("player" + name + "create");
	m_pSceneMgr = sceneMgr;

	m_MoveSpeed = 50;
	m_MaxSpeed = 200;

	m_PlayerShip = new BaseShip(name + "ship", sceneMgr, pPos);

	m_PlayerCamera = m_pSceneMgr->createCamera(name + "GameCamera");

	m_PlayerCamera->setPosition (0, 0, 0);
	m_PlayerCamera->setNearClipDistance(5);
	//m_PlayerCamera->setFOVy(Radian(90));
    m_PlayerCamera->setAspectRatio(Real(OgreFramework::getSingletonPtr()->m_pViewport->getActualWidth()) /
     Real(OgreFramework::getSingletonPtr()->m_pViewport->getActualHeight()));

	m_ExCamera = new ExtendedCamera (name + "Extended Camera", sceneMgr, m_PlayerCamera);
	m_ExCamera->setTightness(0.8);
}


BasePlayer::~BasePlayer(void)
{
}

void BasePlayer::updatePlayer(double timeSinceLastFrame)
{
	m_PlayerShip->updateShip(timeSinceLastFrame);

	//Ogre::Vector3 temp = m_PlayerShip->m_ShipNode->getPosition();
   // m_PlayerCamera->setPosition(Vector3(temp.x, temp.y + 50, temp.z - 30));
	m_ExCamera->update (timeSinceLastFrame, m_PlayerShip->getCameraNode()->_getDerivedPosition(),  m_PlayerShip->getSightNode()->_getDerivedPosition());
}

void BasePlayer::updateTarget(Ogre::Vector3 target)
{
	m_PlayerShip->updateTarget(target);
}

void BasePlayer::updateCamera(Degree yaw, Degree pitch)
{
	m_PlayerShip->updateCamera(yaw, pitch);
}

void BasePlayer::firePrimaryWeapon()
{
	m_PlayerShip->firePrimaryWeapon();
}
void BasePlayer::fireSecondaryWeapon()
{
	m_PlayerShip->fireSecondaryWeapon();
}


void BasePlayer::rotateLeft()
{
	m_PlayerShip->rotateLeft();
}
void BasePlayer::rotateRight()
{
	 m_PlayerShip->rotateRight();
}
void BasePlayer::moveLeft()
{
	m_PlayerShip->moveLeft();
}
void BasePlayer::moveRight()
{
	 m_PlayerShip->moveRight();
}
void BasePlayer::moveDown()
{
	m_PlayerShip->moveDown();
}
void BasePlayer::moveUp()
{
	m_PlayerShip->moveUp();
}

Ogre::Vector3 BasePlayer::getWorldPosition () {
      return m_PlayerShip->m_ShipNode->_getDerivedPosition ();
 }

void BasePlayer::changeDebugVisibility()
{
	m_PlayerShip->changeDebugVisibility();
}